[20220824_155909_user] D2_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};
  40. uint16_t i,val,value_H,value_L,Cvalue,Hvalue;
  41. int error;
  42. float C1=-2.0468;
  43. float C2=0.0367;
  44. float C3=-0.0000015955;
  45. float RH_Lin;
  46. float RH_Ture;
  47. float d1=-39.6;
  48. float d2=0.01;
  49. float T1=0.01;
  50. float T2=0.00008;
  51. float temp_C=0;
  52. int temp;
  53. int RH;
  54. float M14_SHT11 =0;
  55. /* USER CODE BEGIN PV */
  56. typedef void (*pFunction)(void);
  57. /* USER CODE END PV */
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. void MX_ADC1_Init1(char pin);
  61. void sensor(void);
  62. void SHT10_TransStart(void);
  63. void SHT10_WriteByte(void);
  64. void SHT10_WriteByte2(void);
  65. void SHT10_ReadByte(void);
  66. void SHT10_Calculate(void);
  67. void MX_GPIO_Input1(unsigned long pin);
  68. /* USER CODE BEGIN PFP */
  69. /* USER CODE END PFP */
  70. /* Private user code ---------------------------------------------------------*/
  71. /* USER CODE BEGIN 0 */
  72. int _write(int fd, char *ch, int len)
  73. {
  74. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  75. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  76. return len;
  77. }
  78. void print(char* s, float c)
  79. { int w;
  80. int w_d;
  81. int w_f;
  82. if(c<0)
  83. {
  84. w = c*100;
  85. w_d = w/100;
  86. w_f = (-1*w)%100;
  87. }
  88. else
  89. {
  90. w = c*100;
  91. w_d = w/100;
  92. w_f = w%100;
  93. }
  94. printf("%s = %d.%d\n",s,w_d,w_f);
  95. }
  96. /* USER CODE END 0 */
  97. /**
  98. * @brief The application entry point.
  99. * @retval int
  100. */
  101. int main(void)
  102. {
  103. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  104. HAL_Init();
  105. SystemClock_Config();
  106. MX_GPIO_Init();
  107. MX_USART2_UART_Init();
  108. MX_UART4_Init();
  109. MX_USART1_UART_Init();
  110. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  111. HAL_Delay(1000);
  112. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  113. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  114. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  115. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  116. {
  117. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  118. }
  119. else if(zigbeestatu[18]== 0x05)
  120. {
  121. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  122. }
  123. while (1)
  124. {
  125. sensor();
  126. if(M14_SHT11<16)
  127. {
  128. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_RESET);
  129. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  130. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  131. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  132. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  133. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  134. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  135. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  136. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  137. HAL_Delay(5000);
  138. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_SET);
  139. }
  140. }
  141. }
  142. void SystemClock_Config(void)
  143. {
  144. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  145. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  146. __HAL_RCC_PWR_CLK_ENABLE();
  147. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  148. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  149. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  150. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  151. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  152. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  153. RCC_OscInitStruct.PLL.PLLM = 8;
  154. RCC_OscInitStruct.PLL.PLLN = 72;
  155. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  156. RCC_OscInitStruct.PLL.PLLQ = 3;
  157. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  158. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  159. {
  160. Error_Handler();
  161. }
  162. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  163. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  164. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  165. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  166. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  167. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  168. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  169. {
  170. Error_Handler();
  171. }
  172. }
  173. void user_pwm_setvalue(uint16_t value)
  174. {
  175. TIM_OC_InitTypeDef sConfigOC;
  176. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  177. sConfigOC.Pulse = value;
  178. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  179. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  180. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  181. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  182. }
  183. void user_pwm_setvalue2(uint16_t value)
  184. {
  185. TIM_OC_InitTypeDef sConfigOC;
  186. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  187. sConfigOC.Pulse = value;
  188. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  189. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  190. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  191. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  192. }
  193. void sensor(void)
  194. {
  195. Cvalue=0;
  196. Hvalue=0;
  197. value_H=0;
  198. value_L=0;
  199. SHT10_TransStart();
  200. SHT10_WriteByte();
  201. MX_GPIO_Input1(GPIO_PIN_0);
  202. HAL_Delay(250);
  203. if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
  204. {
  205. SHT10_ReadByte();
  206. value_H=val;
  207. SHT10_ReadByte();
  208. value_L=val;
  209. Cvalue = (value_H<< 8 | value_L);
  210. }
  211. SHT10_TransStart();
  212. SHT10_WriteByte2();
  213. MX_GPIO_Input1(GPIO_PIN_0);
  214. HAL_Delay(250);
  215. if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
  216. {
  217. SHT10_ReadByte();
  218. value_H=val;
  219. SHT10_ReadByte();
  220. value_L=val;
  221. Hvalue = (value_H<< 8 | value_L);
  222. }
  223. SHT10_Calculate();
  224. HAL_Delay(10000);
  225. }
  226. void SHT10_TransStart(void)
  227. {
  228. MX_GPIO_Init1();
  229. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  230. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  231. HAL_Delay(10);
  232. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  233. HAL_Delay(10);
  234. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  235. HAL_Delay(10);
  236. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  237. HAL_Delay(10);
  238. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  239. HAL_Delay(10);
  240. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  241. HAL_Delay(10);
  242. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  243. HAL_Delay(10);
  244. }
  245. void SHT10_WriteByte(void)
  246. {
  247. MX_GPIO_Init1();
  248. for (i=0x80;i>0;i/=2)
  249. {
  250. if (i & 0x03)
  251. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  252. else
  253. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  254. HAL_Delay(10);
  255. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  256. HAL_Delay(10);
  257. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  258. HAL_Delay(10);
  259. }
  260. MX_GPIO_Input1(GPIO_PIN_0);
  261. HAL_Delay(10);
  262. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  263. HAL_Delay(10);
  264. error=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
  265. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  266. HAL_Delay(10);
  267. }
  268. void SHT10_WriteByte2(void)
  269. {
  270. MX_GPIO_Init1();
  271. for (i=0x80;i>0;i/=2)
  272. {
  273. if (i & 0x05)
  274. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  275. else
  276. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  277. HAL_Delay(10);
  278. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  279. HAL_Delay(10);
  280. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  281. HAL_Delay(10);
  282. }
  283. MX_GPIO_Input1(GPIO_PIN_0);
  284. HAL_Delay(10);
  285. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  286. HAL_Delay(10);
  287. error=HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
  288. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  289. HAL_Delay(10);
  290. }
  291. void SHT10_ReadByte(void)
  292. {
  293. val=0;
  294. MX_GPIO_Input1(GPIO_PIN_0);
  295. for (i=0x80;i>0;i/=2)
  296. {
  297. HAL_Delay(10);
  298. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  299. HAL_Delay(10);
  300. if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))
  301. val=( val | i );
  302. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  303. }
  304. MX_GPIO_Init1();
  305. if (1)
  306. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  307. else
  308. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  309. HAL_Delay(10);
  310. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  311. HAL_Delay(10);
  312. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  313. HAL_Delay(10);
  314. }
  315. void SHT10_Calculate(void)
  316. {
  317. temp_C=d1+d2*Cvalue;
  318. RH_Lin = C1+C2*Hvalue+C3*Hvalue*Hvalue;
  319. RH_Ture= (temp_C-25)*(T1+T2*Hvalue)+RH_Lin;
  320. if (RH_Ture>100)
  321. RH_Ture= 100;
  322. if (RH_Ture<0.1)
  323. RH_Ture=0.1;
  324. if (temp_C<0)
  325. {
  326. cmd[1]=1;
  327. cmd[2]=-1*temp_C;
  328. temp=(-10*temp_C);
  329. cmd[3]=temp%10;
  330. cmd[4]=RH_Ture;
  331. RH=RH_Ture*10;
  332. cmd[5]=RH%10;
  333. }
  334. else
  335. {
  336. cmd[1]=0;
  337. cmd[2]=temp_C;
  338. temp=(10*temp_C);
  339. cmd[3]=temp%10;
  340. cmd[4]=RH_Ture;
  341. RH=RH_Ture*10;
  342. cmd[5]=RH%10;
  343. }
  344. }
  345. void MX_ADC1_Init1(char pin)
  346. {
  347. ADC_ChannelConfTypeDef sConfig = {0};
  348. hadc1.Instance = ADC1;
  349. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  350. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  351. hadc1.Init.ScanConvMode = DISABLE;
  352. hadc1.Init.ContinuousConvMode = DISABLE;
  353. hadc1.Init.DiscontinuousConvMode = DISABLE;
  354. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  355. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  356. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  357. hadc1.Init.NbrOfConversion = 1;
  358. hadc1.Init.DMAContinuousRequests = DISABLE;
  359. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  360. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  361. {
  362. Error_Handler();
  363. }
  364. sConfig.Channel = pin;
  365. sConfig.Rank = 1;
  366. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  367. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  368. {
  369. Error_Handler();
  370. }
  371. }
  372. void MX_GPIO_Input1(unsigned long pin)
  373. {
  374. GPIO_InitTypeDef GPIO_InitStruct = {0};
  375. __HAL_RCC_GPIOE_CLK_ENABLE();
  376. GPIO_InitStruct.Pin = pin;
  377. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  378. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  379. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  380. }
  381. void Error_Handler(void)
  382. {
  383. }